Welcome Guest | Sign in | Register

Home > Java Programming > Inner Classes > Questions and Answers

01. What is the output for the below code ?

public class Outer {
private int a = 7;

class Inner {
public void displayValue() {
System.out.println("Value of a is " + a);
}
}
}


public class Test {

public static void main(String... args) throws Exception {
Outer mo = new Outer();
Outer.Inner inner = mo.new Inner();
inner.displayValue();

}

}


A. Value of a is 7 B. Compile Error - not able to access private member.
C. Runtime Exception D. Value of a is 8

Answer and Explanation

Answer: Value of a is 7

Explanation:
An inner class instance can never stand alone without a direct relationship to an instance of the outer class.
you can access the inner class is through a live instance of the outer class.
Inner class can access private member of the outer class.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. What is the output for the below code ?

public class Tech {
public void tech() {
System.out.println("Tech");
}

}


public class Atech {

Tech a = new Tech() {
public void tech() {
System.out.println("anonymous tech");
}
};

public void dothis() {
a.tech();

}

public static void main(String... args){
Atech atech = new Atech();
atech.dothis();
}
A. anonymous tech B. Compile Error 
C. Tech D. anonymous tech Tech

Answer and Explanation

Answer: anonymous tech

Explanation:
This is anonymous subclass of the specified class type.
Anonymous inner class ( anonymous subclass ) overriden the Tech super class of tech() method.
Therefore Subclass method will get called.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. class MyOuter {
class MyInner { }
}
after compile how many classes will be created ?
A. MyOuter.class and MyOuter & MyInner.class B. MyOuter & MyInner.class
C. MyOuter.class D. None of the above

Answer and Explanation

Answer: MyOuter.class and MyOuter & MyInner.class

Explanation:
two class files will be created. MyOuter.class MyOuter& MyInner.class

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. What is the output for the below code ?
public abstract class A {
public void printValue(){
System.out.println("A");
}
}
1. public class Test{
2. public static void main (String[] args){
3. A a1 = new A() {
4. };
5. a1.printValue();
6. }
7. }
A. Compilation fails due to an error on line 3 B. A
C. Compilation fails due to an error on line 5 D. null

Answer and Explanation

Answer: A

Explanation:
The A a1 reference variable refers not to an instance of abstract class A, but to an
instance of an anonymous (unnamed) subclass of A. So no compilation error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. Which statement is true?
A. an inner class instance can never stand alone without a direct relationship to an
instance of the outer class
B. you can access the inner class is through a live instance of the outer class
C. Inner class can access private member of the outer class. D. All of the above

Answer and Explanation

Answer: All of the above

Explanation:
an inner class instance can never stand alone without a direct relationship to an
instance of the outer class. you can access the inner class is through a live instance of
the outer class. Inner class can access private member of the outer class.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. What is the output for the below code ?
class Outer {
private String x = "Outer variable";
void doStuff() {
String z = "local variable";
class Inner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Local variable z is " + z);
}
}
}
}
A. Compile Fail : local variable z is can't access from within inner class B. Compilation succeed but Runtime Exception
C. Compile without error. D. None of the above

Answer and Explanation

Answer: Compile Fail : local variable z is can't access from within inner class

Explanation:
the inner class object cannot use the local variables of the method the inner class is in.
only final can be accessible.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. What is the output for the below code ?
class Outer {
private String x = "Outer variable";
void doStuff() {
final String z = "local variable";
class Inner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Local variable z is " + z);
}
}
Inner mi = new Inner();
mi.seeOuter();
}
public static void main(String... args){
Outer out = new Outer();
out.doStuff();
}
}
What is the output?
A. Compile Fail : local variable z is can't access from within inner class B. Outer x is Outer variable Local variable z is local variable
C. Outer x is Outer variable D. Compilation succeed but Runtime Exception

Answer and Explanation

Answer: Outer x is Outer variable Local variable z is local variable

Explanation:
the inner class object cannot use the local variables of the method the inner class is in.
only final can be accessible.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. What is the output for the below code ?
public class Tech {
public void tech() {
System.out.println("Tech");
}
}
public class Atech {
Tech a = new Tech() {
public void nontech () {
System.out.println("anonymous nontech tech");
}
public void tech() {
System.out.println("anonymous tech");
}
};
public void dothis() {
a.tech();
a.nontech();
}
public static void main(String... args){
Atech atech = new Atech();
atech.dothis();
}
}
A. anonymous nontech tech anonymous tech B. anonymous tech
C. Compilation fails D. Compilation succeed but Runtime Exception

Answer and Explanation

Answer: Compilation fails

Explanation:
anonymous subclass of the specified class type. Compile fail because Tech class does not have nontech() method. compiler will complain if you try to invoke any method on an anonymous inner class reference that is not in the superclass class definition.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.